package pub.cleangao.mybatis.expression;

import lombok.Setter;

@Setter
public class ValueExpression implements WhereExpressionValue {

    private WhereJoint whereJoint;
    private String column;
    private Object targetValue;
    private WhereOperator whereOperator;
    private int index;

    public static WhereExpression buildExpression(WhereJoint whereJoint, String columnName, WhereOperator whereOperator, Object value, int... index) {
        ValueExpression valueExpression = new ValueExpression();
        valueExpression.setWhereJoint(whereJoint);
        valueExpression.setColumn(columnName);
        valueExpression.setWhereOperator(whereOperator);
        valueExpression.setTargetValue(value);
        if (index != null && index.length > 0) {
            valueExpression.setIndex(index[0]);
        }
        return valueExpression;
    }

    @Override
    public WhereJoint joint() {
        return whereJoint;
    }

    @Override
    public String column() {
        return column;
    }

    @Override
    public Object targetValue() {
        return targetValue;
    }

    @Override
    public WhereOperator operator() {
        return whereOperator;
    }

    @Override
    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public int index() {
        return index;
    }


    @Override
    public WhereExpressionType type() {
        return WhereExpressionType.VALUE;
    }
}
